<?php
/**
 * Created by dh2y.
 * Blog: http://blog.csdn.net/sinat_22878395
 * Date: 2020/2/6 10:35
 * For:
 */

namespace app\admin\model;


use think\Model;

class AuthRule extends Model
{
    protected $pk = 'id';

    /**
     * 搜索器 搜索id
     * @param $query
     * @param $value
     */
    public function searchIdAttr($query, $value){
        if ($value){
            $ids = AuthRule::where('pid',$value)->column('id');
            foreach ($ids as $menu){
                $id_1 = AuthRule::where('pid',$menu)->column('id');
                $ids = array_merge($ids,$id_1);
                foreach ($id_1 as $action){
                    $id_2 = AuthRule::where('pid',$action)->column('id');
                    $ids = array_merge($ids,$id_2);
                }
            }
            $ids[] = $value;
            $query->where('id','in',$ids);
        }
    }

    /**
     * 搜索器 规则标识
     * @param $query
     * @param $value
     */
    public function searchNameAttr($query, $value){
        if ($value){
            $query->where('name','like','%'.$value.'%');
        }
    }

    /**
     * 搜索器 权限名称
     * @param $query
     * @param $value
     */
    public function searchTitleAttr($query, $value){
        if ($value){
            $query->where('title','like','%'.$value.'%');
        }
    }

    /**
     * 搜索器 审核状态
     * @param $query
     * @param $value
     */
    public function searchStateAttr($query, $value){
        if ($value!=''){
            $query->where('state',$value);
        }
    }

}